import PageModel from "../../../models/Page";
import Crypt from "../../../utils/Crypt";
import ProductModel from "../../../models/Product";

const pageModel = new PageModel()
const productModel = new ProductModel()

Page({
  data: {
    // 商家ID
    merchant_id: 0,
    supplier: {}
  },



  onLoad: function (options) {
    this.setData({
      merchant_id: options.id
    })
    this.getMerchant()
  },

  getMerchant() {
    productModel.getSupplierDesc({ id: this.data.merchant_id }).then(response => {
      let { data } = response
      data = Crypt.parse(data)
      this.setData({
        supplier: {...data.supplier}
      })
      // console.log(this.data.supplier)
    })
  },

  bindAmount(e) {
    this.setData({ amount: e.detail.value.trim()})
  },

  onSave() {
    if (isNaN(+this.data.amount) || +this.data.amount <= 0) {
      return wx.showToast({title: '金额错误', icon: 'none', mask: true})
    }

    pageModel.merchantPayRun({
      merchant_id: this.data.merchant_id,
      amount: this.data.amount
    }).then(response => {

      let { data } = response
      data = Crypt.parse(data)
      // console.log(data)

      let serial = {
        order_id: data.order_id
      }
      serial = JSON.stringify(serial)
      wx.navigateTo({
        url: '/sub_main/pages/pay/index?serial=' + serial
      })

    })
  },


});
